{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `PerspectiveWidget` tutorial\n",
    "\n",
    "`PerspectiveWidget` is a Jupyterlab plugin that allows you to visualize and transform data using the full capabilities of Perspective and `perspective-viewer`, all within a notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date, datetime\n",
    "\n",
    "data = {\n",
    "    \"int\": [i for i in range(4)],\n",
    "    \"float\": [i * 1.25 for i in range(4)],\n",
    "    \"str\": [\"a\", \"b\", \"c\", \"d\"],\n",
    "    \"bool\": [True, False, True, False],\n",
    "    \"date\": [date.today() for i in range(4)],\n",
    "    \"datetime\": [datetime.now() for i in range(4)]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a `PerspectiveWidget`\n",
    "\n",
    "To create an instance of the widget, initialize `PerspectiveWidget` with a `perspective.Table`, a dataset, a schema, or `None`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import string\n",
    "from perspective import Table, PerspectiveWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget = PerspectiveWidget(data, plugin=\"y_line\", row_pivots=[\"str\"], columns=[\"int\", \"float\"])\n",
    "widget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once the widget has been created, you can call `load()` and `update()` on the widget instance, and the widget will reflect the new dataset.\n",
    "\n",
    "When `load()` is called multiple times, the widget compares the column names of the new dataset with the column names of the old dataset. \n",
    "\n",
    "Because attempting to pivot/sort/filter etc. on a column not in the dataset raises an error, the Widget will clear its configuration state if the columns are found to be different."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `PerspectiveWidget.update()`\n",
    "\n",
    "Updating the widget with new data is easy - simply call `update()` on the instance with new data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget.update({\n",
    "    \"int\": [i for i in range(4, 9)],\n",
    "    \"float\": [i * 1.25 for i in range(4, 9)],\n",
    "    \"str\": [random.choice(string.ascii_letters) for i in range(4, 9)],\n",
    "    \"bool\": [True, False, True, False],\n",
    "    \"date\": [date.today() for i in range(4, 9)],\n",
    "    \"datetime\": [datetime.now() for i in range(4, 9)]\n",
    "}) # rendered widget will automatically show new data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
